package cn.huiyunche.base.service.interfaces.impl;

import cn.huiyunche.base.service.interfaces.BDistanceBetweenAddressesService;
import cn.huiyunche.base.service.mappers.BDistanceBetweenAddressesMapper;
import cn.huiyunche.base.service.model.BDistanceBetweenAddresses;
import cn.huiyunche.base.service.model.BDistanceBetweenAddressesExample;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * @FileName: cn.huiyunche.app.service.interfaces.impl
 * @Description: Description
 * @author: Aaron
 * @date: 2016/12/1 下午12:04
 */
@Service
public class BDistanceBetweenAddressesServiceImpl implements BDistanceBetweenAddressesService {

    private static final Logger LOGGER = LoggerFactory.getLogger(BDistanceBetweenAddressesServiceImpl.class);

    @Autowired
    private BDistanceBetweenAddressesMapper addressesMapper;

    @Override
    public int add(BDistanceBetweenAddresses addresses) {
        LOGGER.info("add params : {}", addresses);
        if (null == addresses) {
            LOGGER.error("add param addresses must not be null");
            throw new IllegalArgumentException("地址距离信息不能为空");
        }

        return addressesMapper.insertSelective(addresses);
    }

    @Override
    public BigDecimal getDistanceByLongitudeAndLatitude(String depaLongitudeAndLatitude, String destLongitudeAndLatitude) {
        LOGGER.info("getDistanceByLongitudeAndLatitude params : {}, {}", depaLongitudeAndLatitude, destLongitudeAndLatitude);

        if (StringUtils.isBlank(depaLongitudeAndLatitude)) {
            LOGGER.error("getDistanceByLongitudeAndLatitude param depaLongitudeAndLatitude must not be null");
            throw new IllegalArgumentException("起始地经纬度不能为空");
        }

        if (StringUtils.isBlank(destLongitudeAndLatitude)) {
            LOGGER.error("getDistanceByLongitudeAndLatitude param destLongitudeAndLatitude must not be null");
            throw new IllegalArgumentException("目的地经纬度不能为空");
        }

        BDistanceBetweenAddressesExample example1 = new BDistanceBetweenAddressesExample();
        example1.createCriteria().andDepaLongitudeEqualTo(new BigDecimal(depaLongitudeAndLatitude.split(",")[0]))
                .andDepaLatitudeEqualTo(new BigDecimal(depaLongitudeAndLatitude.split(",")[1]))
                .andDestLongitudeEqualTo(new BigDecimal(destLongitudeAndLatitude.split(",")[0]))
                .andDestLatitudeEqualTo(new BigDecimal(destLongitudeAndLatitude.split(",")[1]));

        List<BDistanceBetweenAddresses> list = addressesMapper.selectByExample(example1);
        if (CollectionUtils.isNotEmpty(list)) {
            return list.get(0).getDistance();
        } else {
            example1 = new BDistanceBetweenAddressesExample();
            example1.createCriteria().andDepaLongitudeEqualTo(new BigDecimal(destLongitudeAndLatitude.split(",")[0]))
                    .andDepaLatitudeEqualTo(new BigDecimal(destLongitudeAndLatitude.split(",")[1]))
                    .andDestLongitudeEqualTo(new BigDecimal(depaLongitudeAndLatitude.split(",")[0]))
                    .andDestLatitudeEqualTo(new BigDecimal(depaLongitudeAndLatitude.split(",")[1]));
            list = addressesMapper.selectByExample(example1);
            if (CollectionUtils.isNotEmpty(list)) {
                return list.get(0).getDistance();
            }
        }

        return null;
    }
}
